appBosch.iEvaluations = 1;
var grid;

$(document).ready(function() {
    var dataSource = new kendo.data.DataSource({
        data: appBosch.data
    });

    grid = $("#grid").kendoGrid({
        toolbar: [
            {
                name: "pdf"
            },
            {
                name: "excel"
            }
        ],
        excel: {
            allPages: true,
            fileName: "Mis cursos.xlsx",
            filterable: true
        },
        pdf: {
            allPages: true,
            fileName: "Mis cursos.pdf"
        },
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        resizable: true,
        columnMenu: true,
        sortable: true,
        dataBound: gridDataBound,
        dataSource: dataSource,
        groupable: true,
        schema: {
            model: {
                id: "value",
                fields: {
                    test: {type: 'string'},
                    calif: {type: 'string'},
                    dateEval: {type: 'string'}
                }
            }
        },
        columns: [
            {
                title: "#",
                template: '#=appBosch.iEvaluations++#',
                width: 50
            },
            {field: "test", title: "Curso"},
            {field: "calif", title: "Calificación"},
            {
                field: "dateEval",
                title: "Fecha de evaluación",
                template: '<span title="#= dateEval != null ? kendo.toString(new Date(dateEval.replace(/-/g, "/")), "D") : "Sin fechar" #">#= dateEval != null ? kendo.toString(new Date(dateEval.replace(/-/g, "/")), "d") : "Sin fechar" #</span>'
            },
            {
                command: [
                    {
                        name: "attempts",
                        text: "Ver intentos",
                        click: seeAttempts
                    },
                    {
                        name: "details",
                        text: "Ver detalles",
                        click: seeDetails
                    }]
            }
        ]
    }).data('kendoGrid');
    $('#grid table').on('dblclick', 'tr', seeDetails2);
    $('body').on('click', '.seeCert', showCertificate);
});

function gridDataBound() {
    appBosch.iEvaluations = 1;
    $("#grid").kendoTooltip({
        filter: "span.certdet",
        content: {
            url: appBosch.urlDetails
        },
        width: 400,
        position: "top",
        requestStart: function(e) {
            e.options.url = appBosch.urlDetails + '/' + e.target.data("id");
        }
    });
}

function seeAttempts(e) {
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var id = dataItem.value;
    var contentTemp = $("#windowTemplate").html();
    var temp = kendo.template(contentTemp);

    $.get(appBosch.urlUserAttempts + '/' + id, {}, function (res) {
        dialog = $('<div id="windowCalifAttempt" class="window" ></div>');
        var info = { res: res };
        dialog.html(temp(info));
        dialog.kendoWindow({
            modal: true,
            title: dataItem.test,
            minWidth: 450,
            deactivate: function(e) {
                dialog.data("kendoWindow").destroy();
            }
        }).data("kendoWindow").open().center();
    });
}

function seeDetails(e) {
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var id = dataItem.value;
    var contentTemp = $("#windowTemplateDetails").html();
    var temp = kendo.template(contentTemp);

    $.get(appBosch.urlEvaluationdetails + '/' + id, {}, function (res) {
        dialog = $('<div id="windowDetails" class="window" ></div>');
        var info = { res: res };
        dialog.html(temp(info));
        dialog.append('<input class="seeCert k-button k-button-icontext" data-id="' + id + '" value="Ver Diploma" />');
        dialog.kendoWindow({
            modal: true,
            title: dataItem.test,
            minWidth: 500,
            deactivate: function(e) {
                dialog.data("kendoWindow").destroy();
            }
        }).data("kendoWindow").open().center();
    });
}

function seeDetails2(e) {
    var selectedRows = grid.select();
    var dataItem = grid.dataItem(selectedRows[0]);
    var id = dataItem.value;
    var contentTemp = $("#windowTemplateDetails").html();
    var temp = kendo.template(contentTemp);

    $.get(appBosch.urlEvaluationdetails + '/' + id, {}, function (res) {
        dialog = $('<div id="windowDetails" class="window" ></div>');
        var info = { res: res };
        dialog.html(temp(info));
        dialog.append('<input class="seeCert k-button k-button-icontext" data-id="' + id + '" value="Ver Diploma" />');
        dialog.kendoWindow({
            modal: true,
            title: dataItem.test,
            minWidth: 500,
            deactivate: function(e) {
                dialog.data("kendoWindow").destroy();
            }
        }).data("kendoWindow").open().center();
    });
}

function showCertificate(e)
{
    var idCert = $(this).data('id');
    var url = appBosch.urlDiploma + '/' + idCert;
    $('#magicLink').attr('href', url);
    $('#magicLink').attr('target', '_blank');
    $('#magicLink')[0].click();
}